﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Caliburn.Micro;
using MvvmEx.Command;

namespace MvvmEx.Base
{
    /// <summary>
    /// 树基类
    /// </summary>
    public class TreeNodeViewModelBase : PropertyChangedBase
    {
        #region 字段
        private string _displayName;
        private ObservableCollection<TreeNodeViewModelBase> _children;
        private bool _isExpanded;
        private bool _isSelected;
        private ObservableCollection<MenuItemCommand> _contextMenuItemCommands;
        private int _level = 1;
        private bool _isShow = true;
        private bool _isEdit;
        private bool _hasChildren;
        #endregion

        #region 属性
        /// <summary>
        /// 是否具有子集
        /// </summary>
        public bool HasChildren
        {
            get
            {
                return Children.Any();
            }
            set
            {
                if (value == _hasChildren) return;
                _hasChildren = value;
                NotifyOfPropertyChange(() => HasChildren);
            }
        }

        /// <summary>
        /// 树的层级
        /// </summary>
        public int Level
        {
            get
            {
                return _level;
            }
            set
            {
                if (value == _level) return;
                _level = value;
                NotifyOfPropertyChange(() => Level);
            }
        }

        /// <summary>
        /// 右键命令菜单
        /// </summary>
        public ObservableCollection<MenuItemCommand> ContextMenuItemCommands
        {
            get
            {
                return _contextMenuItemCommands;
            }
            set
            {
                _contextMenuItemCommands = value;
                NotifyOfPropertyChange(() => ContextMenuItemCommands);
            }
        }

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;

                if (value)
                {
                    Selected();
                }
                else
                {
                    UnSelected();
                }

                NotifyOfPropertyChange(() => IsSelected);
            }
        }

        /// <summary>
        /// 是否正在编辑
        /// </summary>
        public bool IsEdit
        {
            get
            {
                return _isEdit;
            }
            set
            {
                if (value == _isEdit) return;
                _isEdit = value;
                NotifyOfPropertyChange(() => IsEdit);
            }
        }

        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsShow
        {
            get
            {
                return _isShow;
            }
            set
            {
                if (value == _isShow) return;
                _isShow = value;
                NotifyOfPropertyChange(() => IsShow);
            }
        }

        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpanded
        {
            get
            {
                return _isExpanded;
            }
            set
            {
                _isExpanded = value;
                NotifyOfPropertyChange(() => IsExpanded);
            }
        }

        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName
        {
            get
            {
                return _displayName;
            }
            set
            {
                _displayName = value;
                NotifyOfPropertyChange(() => DisplayName);
            }
        }

        /// <summary>
        /// 父级
        /// </summary>
        public TreeNodeViewModelBase Parent
        {
            get;
            set;
        }

        /// <summary>
        /// 子集
        /// </summary>
        public ObservableCollection<TreeNodeViewModelBase> Children
        {
            get
            {
                return _children;
            }
            set
            {
                _children = value;
                NotifyOfPropertyChange(() => Children);
            }
        }

        #endregion

        #region 委托
        /// <summary>
        /// 选中委托
        /// </summary>
        public Action<TreeNodeViewModelBase> SelectedAction
        {
            get; set;
        }

        /// <summary>
        /// 没有选中
        /// </summary>
        public Action<TreeNodeViewModelBase> UnSelectedAction
        {
            get; set;
        }

        #endregion

        #region 构造
        /// <summary>
        /// 构造
        /// </summary>
        public TreeNodeViewModelBase()
        {
            Children = new ObservableCollection<TreeNodeViewModelBase>();
            ContextMenuItemCommands = new ObservableCollection<MenuItemCommand>();
        }
        #endregion

        #region 公有方法

        #region 添加子集

        public void AddChildren(TreeNodeViewModelBase child)
        {
            child.Level = this.Level + 1;
            child.Parent = this;
            Children.Add(child);
        }

        #endregion

        #region 展开自身一直向上的层级

        public void ExpandToTop()
        {
            IsExpanded = true;

            if (Parent != null)
            {
                Parent.ExpandToTop();
            }
        }

        #endregion

        #region 展开第一层级

        /// <summary>
        /// 第一层级展开
        /// </summary>
        public void FirstChildrenExpand()
        {
            this.IsExpanded = true;

            if (this.Children.Any())
            {
                var firstChildren = this.Children.FirstOrDefault();

                if (firstChildren != null)
                {
                    firstChildren.FirstChildrenExpand();
                }
            }
        }

        #endregion

        #region 获取所有子集

        public List<TreeNodeViewModelBase> GetAllChild()
        {
            var result = new List<TreeNodeViewModelBase>();

            if (this.Children.Any())
            {
                result.AddRange(Children);

                foreach (var treeNodeViewModelBase in Children)
                {
                    var subResult = treeNodeViewModelBase.GetAllChild();
                    result.AddRange(subResult);
                }

                return result;
            }
            else
            {
                return result;
            }
        }

        #endregion

        #region 私有方法

        #region 获取当前节点的层级

        private void GetLevel(ref int level)
        {
            if (this.Parent != null)
            {
                level++;
                Parent.GetLevel(ref level);
            }
            else
            {
                level++;
            }
        }

        #endregion

        #region 保护方法

        /// <summary>
        /// 选中事件
        /// </summary>
        protected virtual void Selected()
        {
            if (SelectedAction != null)
            {
                SelectedAction(this);
            }
        }

        /// <summary>
        /// 取消选中事件
        /// </summary>
        protected virtual void UnSelected()
        {
            if (UnSelectedAction != null)
            {
                UnSelectedAction(this);
            }
        }

        #endregion

        #endregion

        #endregion
    }
}